package com.kuangjia.student_ems.repository;

import com.kuangjia.student_ems.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
    
    Optional<Department> findByName(String name);
    
    boolean existsByName(String name);
    
    @Query("SELECT d.id, d.name, COUNT(u) FROM Department d LEFT JOIN d.users u WHERE u.role <> 'HR_ADMIN' OR u.role IS NULL GROUP BY d.id ORDER BY d.id")
    List<Object[]> getDepartmentsWithEmployeeCount();
    
    @Query("SELECT d FROM Department d WHERE LOWER(d.name) LIKE LOWER(:keyword) OR LOWER(d.description) LIKE LOWER(:keyword)")
    List<Department> findByNameContainingIgnoreCaseOrDescriptionContainingIgnoreCase(@Param("keyword") String keyword, @Param("keyword") String keyword2);
} 